package com.qingsong.controller;

import com.mybatisflex.core.paginate.Page;
import com.qingsong.constant.ML;
import com.qingsong.dto.EpisodeExcelDTO;
import com.qingsong.dto.EpisodeInsertDTO;
import com.qingsong.dto.EpisodePageDTO;
import com.qingsong.dto.EpisodeUpdateDTO;
import com.qingsong.entity.Episode;
import com.qingsong.entity.Episode;
import com.qingsong.result.Result;
import com.qingsong.util.EasyExcelUtil;
import com.qingsong.vo.EpisodeSimpleListVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.qingsong.service.EpisodeService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 集次表 控制层。
 *
 * @author qingsong
 * @since 2024-12-22
 */
@Tag(name = "集次")
@RestController
@RequestMapping("/episode")
public class EpisodeController {

    @Autowired
    private EpisodeService episodeService;

    @Operation(summary = "新增 - 单条", description = "新增一条集次记录")
    @PostMapping("insert")
    public boolean insert(@Validated @RequestBody EpisodeInsertDTO dto) {
        return episodeService.insert(dto);
    }

    @Operation(summary = "查询 - 单条", description = "按主键查询一条集次记录")
    @GetMapping("select/{id}")
    public Episode select(@PathVariable("id") Long id) {
        return episodeService.select(id);
    }

    @Operation(summary = "查询 - 简单列表", description = "查询全部集次记录,仅返回简单信息")
    @GetMapping("simpleList")
    public List<EpisodeSimpleListVO> simpleList() {
        return episodeService.simpleList();
    }

    @Operation(summary = "查询 - 分页", description = "分页查询集次记录")
    @GetMapping("page")
    public Page<Episode> page(@Validated EpisodePageDTO dto) {
        return episodeService.page(dto);
    }

    @Operation(summary = "修改 - 单条", description = "按主键修改一条集次记录")
    @PutMapping("update")
    public boolean update(@RequestBody EpisodeUpdateDTO dto) {
        return episodeService.update(dto);
    }

    @Operation(summary = "删除 - 单条", description = "按主键删除一条集次记录")
    @DeleteMapping("delete/{id}")
    public boolean delete(@PathVariable("id") Long id) {
        return episodeService.delete(id);
    }

    @Operation(summary = "删除 - 批量", description = "按主键批量删除集次记录")
    @DeleteMapping("deleteBatch")
    public boolean deleteBatch(@RequestParam("ids") List<Long> ids) {
        return episodeService.deleteBatch(ids);
    }

    @Operation(summary = "修改 - 集次视频", description = "按主键修改集次视频")
    @PostMapping("/uploadVideo/{id}")
    public Result<String> uploadVideo(@PathVariable("id") Long id, @RequestParam("videoFile") MultipartFile videoFile) {
        return new Result<>(episodeService.uploadVideo(videoFile, id));
    }

    @Operation(summary = "修改 - 集次封面", description = "按主键修改集次封面")
    @PostMapping("/uploadVideoCover/{id}")
    public Result<String> uploadVideoCover(@PathVariable("id") Long id, @RequestParam("videoCoverFile") MultipartFile videoCoverFile) {
        return new Result<>(episodeService.uploadVideoCover(videoCoverFile, id));
    }

    @Operation(summary = "查询 - 报表打印", description = "打印集次相关的报表数据")
    @GetMapping("excel")
    public void excel(HttpServletResponse response) {
        EasyExcelUtil.download(response,
                ML.Episode.EXCEL_FILE_NAME,
                ML.Episode.EXCEL_SHEET_NAME,
                EpisodeExcelDTO.class,
                episodeService.excel());
    }

}
